<template>
  <div style="position: relative">
    <div class="search-box v-flex" :class="{ 'v-shadow': !hideShadow }" :style="searchBarStyle">
      <input
        ref="searchInput"
        required
        type="text"
        class="search-txt"
        :value="modelValue"
        :placeholder="placeholder"
        @input="onInput($event)"
        @focus="isFocus = true"
        @blur="onSearchBlur"
      />
      <div class="search-btn" :class="{ 'v-shadow': !hideShadow }" v-ripple @click="search">
        <v-icon icon="icon-search" color="#e84118" />
      </div>
    </div>
    <!-- 搜索建议 -->
    <transition name="search">
      <ul v-if="hasAutoplate" class="search-autocomplate v-radius v-shadow v-blur">
        <li v-for="item of messageList" class="search-autocomplate-item v-radius">
          <apple-button
            block
            hide-shadow
            :border="false"
            :styles="{ width: '100%', background: 'transparent', textAlign: 'left', padding: '8px', height: 'auto', color: primaryColor }"
            @click="onClickAutocomplate(item)"
          >
            <div>
              <div class="v-flex v-justify-between">
                <span v-if="isObject(item)" class="autocomplate-item-txt">{{ item[options.label] }}</span>
                <span v-else class="autocomplate-item-txt">{{ item }}</span>
              </div>
              <div v-if="item.hint" class="autocomplate-hint v-flex">
                <span v-if="item.icon" v-icon="item.icon" class="v-m-r"></span>
                <span>{{ item.hint }}</span>
              </div>
            </div>
          </apple-button>
        </li>
      </ul>
    </transition>
  </div>
</template>

<script lang="ts">
// name: "SearchBar"
import { computed, ref, defineProps, inject, defineEmit, PropType, ComputedRef, Ref, defineComponent } from "vue";
import { useRouter } from 'vue-router';
import VIcon from '@/components/Icon.vue';
import AppleButton from '@/components/apple/AppleButton.vue';
import { isObject } from '@/utils';
import searchQuery from '@/json/search.json';
import { primaryColor } from '@/config/layout.ts';

interface IOptions {
  label: string;
  value?: string;
  cmd?: string;
  icon?: string;
  desc?: string;
  hint?: string;
}

export default defineComponent({
  components: {
    VIcon,
    AppleButton
  },
  props: {
    autocomplate: {
        type: Boolean,
        default: true
      },
      placeholder: {
        type: String,
        default: "请输入"
      },
      inline: {
        type: Boolean,
        default: false
      },
      modelValue: {
        type: String,
        default: ""
      },
      width: {
        type: String,
        default: "240px"
      },
      querySearch: {
        type: Array,
        default: () => []
      },
      options: {
        type: Object as PropType<IOptions>,
        default: () => ({ label: 'label', value: 'value', cmd: '' })
      },
      hideShadow: {
        type: Boolean,
        default: false
      }
  },
  emits: ['search', 'update:modelValue'],
  setup(props, context) {
    const emit = context.emit;
    const router = useRouter();
    const g_bg = inject('$bg') as Ref<string>;
    const g_color = inject('$color') as Ref<string>;
    const isFocus = ref(false);
    const searchInput = ref(null);
    const baseQuery = searchQuery;
    const search = () => emit('search', props.modelValue);
    const searchBarStyle = computed(() => {
      const styles: any = {};
      if (props.inline) {
        styles.display = "inline-flex";
      }
      if (props.modelValue || isFocus.value) {
        styles.width = props.width;
        styles.boxShadow = '0 3px 1px -2px rgba(0,0,0,.2),0 2px 2px 0 rgba(0,0,0,.14),0 1px 5px 0 rgba(0,0,0,.12) !important';

      }
      return styles;
    });
    const hasAutoplate = computed(() => props.autocomplate && (isFocus.value || props.modelValue !== ''));
    const messageList: ComputedRef<Array<IOptions>> = computed(() => {
      const baseQueryFilter = baseQuery.filter((item: IOptions) => (item.label.includes(props.modelValue) || item.hint?.includes(props.modelValue)));
      return (props.querySearch.concat(baseQueryFilter) as Array<IOptions>);
    });
    const bg = computed(() => g_bg.value === '#333' ? 'rgba(33, 33, 33, .5)' : 'rgba(255, 255, 255, .5)');
    const txt_color = computed(() => g_color.value === '#333' ? '#606266' : g_color.value);
    const txt_hover_bg = computed(() => g_bg.value === '#333' ? 'rgba(0, 0, 0, .3)' : '#e5e5e4');
    const onInput = (event: Event) => emit('update:modelValue', (event.target as HTMLInputElement).value);
    const onClickAutocomplate = (item: any) => {
      if (item.cmd !== '') {
        eval(item.cmd);
      }
    };
    const onSearchBlur = () => {
      setTimeout(() => isFocus.value = false, 200)
    }
    return {
      isObject,
      primaryColor,
      router,
      g_bg,
      g_color,
      isFocus,
      searchInput,
      baseQuery,
      search,
      searchBarStyle,
      hasAutoplate,
      messageList,
      bg,
      txt_color,
      txt_hover_bg,
      onInput,
      onClickAutocomplate,
      onSearchBlur
    }
  }
})

</script>

<style scoped lang="scss">
.search-box {
  position: relative;
  justify-content: space-between;
  background-color: v-bind(g_bg);
  height: 40px;
  width: 40px;
  border-radius: 30px;
  transition: all 0.25s ease-out;
}
.search-box:hover {
  width: v-bind(width);
  box-shadow: 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 2px 2px 0 rgba(0, 0, 0, 0.14),
    0 1px 5px 0 rgba(0, 0, 0, 0.12) !important;
}

.search-autocomplate {
  position: absolute;
  max-height: 280px;
  height: 280px;
  width: 100%;
  overflow: auto;
  top: 45px;
  left: 0;
  padding: 8px;
  width: v-bind(width);
  color: v-bind(g_color);
  background-color: v-bind(bg);
  z-index: 1;
}

.search-autocomplate-item {
  cursor: pointer;
  transition: all 0.25s ease-out;
}

.search-autocomplate-item:hover {
  background-color: v-bind(txt_hover_bg);
}

.autocomplate-item-txt {
  font-size: 14px;
  color: v-bind(txt_color);
}

.search-txt {
  border: none;
  background: none;
  outline: none;
  padding: 0;
  color: v-bind(g_color);
  font-size: 14px;
  transition: all 0.25s ease-out;
  width: 0px;
}
.search-btn {
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: v-bind(bg);
  transition: all 0.25s ease-out;
  border: 1px solid v-bind(bg);
  color: $primary-color;
}
/* 当输入框聚焦或者有值的时候都会展开 */
.search-txt:valid,
.search-txt:focus,
.search-box:hover > .search-txt {
  width: calc(100% - 40px);
  padding: 10px;
}

.search-txt:valid ~ .search-btn,
.search-txt:focus ~ .search-btn,
.search-box:hover > .search-btn {
  background-color: white;
  box-shadow: 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 2px 2px 0 rgba(0, 0, 0, 0.14),
    0 1px 5px 0 rgba(0, 0, 0, 0.12) !important;
}

.search-enter-active {
  transition: all 0.3s ease-out;
}

.search-leave-active {
  transition: all 0.3s cubic-bezier(1, 0.5, 0.8, 1);
}

.search-enter-from,
.search-leave-to {
  transform: translateX(20px);
  opacity: 0;
}
.autocomplate-hint {
  font-size: 10px;
  color: #999;
}
</style>